/**
 * 
 */
package com.yunyike.controller.promotion;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yunyike.constants.CommonConstant;
import com.yunyike.constants.RequestConstant;
import com.yunyike.constants.ResponseConstant;
import com.yunyike.controller.BaseController;
import com.yunyike.dto.PromotionDTO;
import com.yunyike.response.Response;
import com.yunyike.service.IPromotionMerchandiseMappingService;

/**
 * @author zhengqiang.shi
 * 2016-4-11 上午11:13:29
 */
@Controller
@RequestMapping(value=RequestConstant.PROMOTION)
public class PromotionController extends BaseController{

	private static Logger log = LoggerFactory.getLogger(PromotionController.class);
	
	@Autowired
	private IPromotionMerchandiseMappingService promotionMerchandiseMappingService;
	
	/**
	 * 根据活动id，获取默认显示的活动商品
	 * @param promotionId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/{promotionId}"+RequestConstant.PROMOTION_DEFAULT)
	public void findItemByPromotionIdDefault(@PathVariable String promotionId,
			HttpServletResponse response){
		
		// response object
		Response res = new Response();
		
		PromotionDTO promotionDTO;
		try {
			promotionDTO = promotionMerchandiseMappingService.findDefaultShowItemByPromotionId(promotionId);
		} catch (Exception e) {
			log.error("promotionMerchandiseMappingService findDefaultShowItemByPromotionId has error.message:"+e.getMessage(),e);
			res.getResult().setCode(CommonConstant.NUMBER__3);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__3));
			returnDatas(res, response);
			return;
		}
		
		if(promotionDTO == null){
			res.getResult().setCode(CommonConstant.NUMBER__2);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__2));
			returnDatas(res, response);
			return;
		}
		
		res.setData(promotionDTO);
		returnDatas(res, response);
		return;
	}
	
	/**
	 * 根据活动id，获取活动商品
	 * @param promotionId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/{promotionId}")
	public void findItemByPromotionId(@PathVariable String promotionId,
			HttpServletResponse response){
		
		// response object
		Response res = new Response();
		
		PromotionDTO promotionDTO;
		try {
			promotionDTO = promotionMerchandiseMappingService.findItemByPromotionId(promotionId);
		} catch (Exception e) {
			log.error("promotionMerchandiseMappingService.findItemByPromotionId has error.message:"+e.getMessage(),e);
			res.getResult().setCode(CommonConstant.NUMBER__3);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__3));
			returnDatas(res, response);
			return;
		}
		
		if(promotionDTO == null){
			res.getResult().setCode(CommonConstant.NUMBER__2);
			res.getResult().setMessage(ResponseConstant.message.get(CommonConstant.NUMBER__2));
			returnDatas(res, response);
			return;
		}
		
		res.setData(promotionDTO);
		returnDatas(res, response);
		return;
	}
}
